﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.04.17
 * 描 述：定时任务管理
 */
var refreshGirdData; // 更新数据
var selectedRow;
var bootstrap = function ($, learun) {
    "use strict";
    var page = {
        init: function () {
            page.initGrid();
            page.bind();
        },
        bind: function () {
            // 查询
            $('#btn_Search').on('click', function () {
                var keyword = $('#txt_Keyword').val();
                page.search({ keyword: keyword });
            });
            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
            // 搜索框初始化
            $('#txt_Keyword').on("keypress", function (e) {
                if (event.keyCode == "13") {
                    var $this = $(this);
                    var keyword = $this.val();
                    page.search({keyword: keyword});
                }
            });
            // 编辑
            $('#lr_edit').on('click', function () {
                selectedRow = $('#gridtable').jfGridGet('rowdata');
                var keyValue = $('#gridtable').jfGridValue('cronId');
                if (learun.checkrow(keyValue)) {
                    learun.layerForm({
                        id: 'Form',
                        title: '编辑定时任务',
                        url: config.webportal_server + 'pages/system/cron/form.html?access_token=' + config.getToken().access_token,
                        width: 600,
                        height: 600,
                        callBack: function (id) {
                            return top[id].acceptClick(refreshGirdData);
                        }
                    });
                }
            });

        },
        initGrid: function () {
            $('#gridtable').jfGrid({
                url: config.base_server() + 'crons/list?access_token=' + config.getToken().access_token,
                headData: [
                    { label: "编号", name: "cronId", width: 80, align: "center" },
                    { label: "任务完整类名", name: "cronKey", width: 350, align: "left" },
                    { label: "cron表达式", name: "cronExpression", width: 150, align: "center" },
                    {
                        label: "状态", name: "status", width: 80, align: "center",
                        formatter: function (value) {
                            if(value == '1') {
                               return  '<span class="label label-success">正常</span>';
                            } else {
                               return  '<span class="label label-default">停止</span>';
                            }
                        }
                     },
                    { label: "任务描述", name: "taskExplain", width: 200, align: "left"},
                    {
                        label: "操作", name: "operate", width: 100, align: "center", sort: true,
                        formatter: function (cellvalue, row, op, $cell) {
                            $cell.on('click', '.trigger', function () {
                                 if ($cell.html().trim() == '') {
                                     return false;
                                 }
                                 var keyvalue = row.cronId;
                                 if (learun.checkrow(keyvalue)) {
                                    learun.layerForm({
                                        id: 'trigger_history',
                                        title: '手工触发执行任务',
                                        url: config.webportal_server + 'pages/system/cron/trigger_date_form.html?access_token=' + config.getToken().access_token,
                                        width: 380,
                                        height: 300,
                                        callBack: function (id) {
                                            var configData = top[id].acceptClick();
                                            if (!!configData) {
                                                 learun.loading(true, '操作进行中...');
                                                 top.learun.loading(true, '操作进行中...');
                                                 learun.postForm(config.base_server() + 'scheduleManuals/trigger?cronId=' + keyvalue + '&startTime=' + configData.excuteTime + '&access_token=' + config.getToken().access_token, {}, function () {
                                                      refreshGirdData();
                                                 });
                                            }
                                        }
                                    });
                                 }
                                 return false;
                            });

                            var html = '';
                            html += '<span class="trigger" style="cursor: pointer;color: green;"><i class="fa fa-play" ></i></span>';
                            return html;
                        }
                    },
                    {
                      label: "上次执行时间", name: "lastExcuteTime", width: 250, align: "center",
                      formatter: function (value, row) {
                        if(!!row.lastExcuteTime) {
                            var label = "label-success";
                            var isSuccess = '成功';
                            if(row.lastExcuteStatus == '0') {
                                isSuccess = '失败';
                                label = "label-danger";
                            }
                            return '<span class="label '+ label +'">' + isSuccess + '</span>' + learun.formatDate(value, 'yyyy-MM-dd hh:mm:ss') + "(耗时"+ row.timeConsuming +"分钟)";
                        } else {
                           return learun.formatDate(value, 'yyyy-MM-dd hh:mm:ss');
                        }
                      }
                    },
                    { label: "异常日志", name: "lastExcuteLog", width: 200, align: "left"}
                ],
                isPage: true,
                reloadSelected: true,
                isTree: false,
                mainId: 'cronId',
            });
            page.search();
        },
        search: function (param) {
            $('#gridtable').jfGridSet('reload', param);
        }
    };

    // 保存数据后回调刷新
    refreshGirdData = function () {
        page.search();
    }

    page.init();
}


